%matplotlib notebook
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import rankdata
from IPython.display import display, display_markdown
def display_md(md, **kwargs):
return display_markdown(md, raw=True, **kwargs)
sns.set(style='whitegrid', palette='Set2')
# unigram
df_uni = pd.read_csv('dedup.en.words.unigrams.tsv', sep='\t')
total_unigrams = df_uni['unigram_freq'].sum()
display_md(f'Total number of unigrams: {total_unigrams}')
df_uni['unigram_freq'] = df_uni['unigram_freq'] / total_unigrams
df_uni['log_unigram_freq'] = np.log10(df_uni['unigram_freq'])
df_uni = df_uni.drop(columns='unigram_freq')
display(df_uni.head())
# bigram
df_bi = pd.read_csv('dedup.en.words.bigrams.tsv', sep='\t')
total_bigrams = df_bi['bigram_freq'].sum()
df_bi = df_bi[df_bi['bigram_freq'] > 1]
display_md(f'Total number of bigrams: {total_bigrams} (we threw some low-frequency ones away)')
df_bi['bigram_freq'] = df_bi['bigram_freq'] / total_unigrams
df_bi['log_bigram_freq'] = np.log10(df_bi['bigram_freq'])
df_bi = df_bi.drop(columns='bigram_freq')
display(df_bi.head())
# word1/word2
df_bi['word1'] = df_bi['bigram'].apply(lambda x: x.split(' ')[0])
df_bi['word2'] = df_bi['bigram'].apply(lambda x: x.split(' ')[1])
df_bi = df_bi.merge(df_uni.rename(columns={'unigram': 'word1', 'log_unigram_freq': 'log_word1_freq'}), how='left', on='word1')
df_bi = df_bi.merge(df_uni.rename(columns={'unigram': 'word2', 'log_unigram_freq': 'log_word2_freq'}), how='left', on='word2')
display(df_bi.head())
# ftp/btp
df_bi['log_ftp'] = df_bi['log_bigram_freq'] - df_bi['log_word1_freq']
df_bi['log_btp'] = df_bi['log_bigram_freq'] - df_bi['log_word2_freq']
df_bi['pmi'] = df_bi['log_bigram_freq'] - df_bi['log_word1_freq'] - df_bi['log_word2_freq']
display(df_bi.head())
# store df
df_bi.to_csv('full_bigram_data.tsv', sep='\t', index=False)
# load df
df_bi = pd.read_csv('full_bigram_data.tsv', sep='\t')
g = sns.PairGrid(df_bi)
g = g.map_diag(plt.hist)
def hexbin(x, y, color, **kwargs):
cmap = sns.light_palette(color, as_cmap=True)
plt.hexbin(x, y, gridsize=15, cmap=cmap, **kwargs)
g = g.map_offdiag(hexbin)
g = sns.distplot(df_uni['log_unigram_freq'], kde=False)
g.set(yscale='log');
g = sns.distplot(df_bi['log_bigram_freq'], kde=False)
g.set(yscale='log');
x = df_bi['log_word1_freq']
y = df_bi['log_word2_freq']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
x = df_bi['log_word1_freq']
y = df_bi['log_bigram_freq']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
x = df_bi['log_word2_freq']
y = df_bi['log_bigram_freq']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
x = df_bi['log_word1_freq']
y = df_bi['log_ftp']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
x = df_bi['log_word2_freq']
y = df_bi['log_ftp']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
x = df_bi['log_word1_freq']
y = df_bi['log_btp']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
x = df_bi['log_word2_freq']
y = df_bi['log_btp']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
x = df_bi['log_ftp']
y = df_bi['log_btp']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
x = df_bi['log_ftp']
y = df_bi['pmi']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
x = df_bi['log_btp']
y = df_bi['pmi']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
x = df_bi['log_word1_freq']
y = df_bi['pmi']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
x = df_bi['log_word2_freq']
y = df_bi['pmi']
g = sns.jointplot(x, y, kind='hex', alpha=.8)
sns.regplot(x, y, ax=g.ax_joint, scatter=False, ci=None, color='black');
ev_btp = np.sum(df_bi['log_word1_freq'] * df_bi['log_btp'])
display_md(f'EV for BTP: {ev_btp.round(2)}')
ev_ftp = np.sum(df_bi['log_word2_freq'] * df_bi['log_ftp'])
display_md(f'EV for FTP: {ev_ftp.round(2)}')
# convert this Jupyter notebook
import subprocess as sp
make_md = 'jupyter nbconvert transitional_probabilities.ipynb --to html --output transitional_probabilities.html'.split(' ')
convert = sp.run(make_md)
if convert.returncode == 0:
display_md('Jupyter notebook converted successfully.')
else:
display_md('Error: encountered problem converting Jupyter notebook')